<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Story
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Story_Model_Chapter extends Core_Model_Item_Abstract
{
	public function getStory(){
		$story = Engine_Api::_()->getItem('story', $this->story_id);
		
		return $story;
	}
	
	public function getHref($params = array()){
		$slug = $this->getStory()->getSlug();
		$params = array_merge(array(
				'route' => 'story_entry_view',
				'reset' => true,				
				'story_id' => $this->story_id,
				'slug' => $slug,
				'chapter_id' => $this->chapter_id
		), $params);
		$route = $params['route'];
		$reset = $params['reset'];
		unset($params['route']);
		unset($params['reset']);
		return Zend_Controller_Front::getInstance()->getRouter()
		->assemble($params, $route, $reset);
	}
	
	public function getOrder(){
		$story = $this->getStory();
		$index = 1;
		if($story){
			$chapters = $story->getChapters();
			foreach($chapters as $chapter){
				if($this->chapter_id == $chapter->chapter_id){
					break;
				}
				$index++;
			}			
		}		
		
		return $index;
	}

	public function canEditThread($viewer){
		if(!$viewer) return false;
		
		$isOwner = $this->isOwner($viewer);
		
		return $isOwner && ($this->category_id == Story_Model_Category::STORY_THREAD);
	}
	
}
